<template>
  <div>
    <div class="p-4">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <el-form inline>
            <el-form-item>
              <el-button
                icon="el-icon-plus"
                type="success"
                @click="deBounceWrap(handleAdd)"
              >新增
              </el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <el-table :data="data" border>
          <el-table-column
            label="序号"
            prop="id"
            width="150px"
          ></el-table-column>
          <el-table-column label="签到天数/天" prop="days"></el-table-column>
          <el-table-column label="礼包内容" prop="name"></el-table-column>
          <el-table-column label="展示数量" prop="number"></el-table-column>
          <el-table-column label="展示图片" prop="picture">
            <template slot-scope="{ row }">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img
                  :src="row.picture"
                  alt=""
                  style="max-width: 200px; max-height: 200px"
                />
                <img
                  slot="reference"
                  :src="row.picture"
                  alt=""
                  style="width: 50px"
                />
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="礼包类型" prop="type">
            <template slot-scope="{ row }">
              <el-tag v-if="row.type === 1" type="primary">道具包</el-tag>
              <el-tag v-if="row.type === 2" type="primary">道具</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="道具id" prop="prop_id"></el-table-column>
          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button
                icon="el-icon-edit"
                plain
                size="mini"
                type="primary"
                @click="deBounceWrap(handleEdit, scope.row)"
              >编辑
              </el-button
              >

              <el-button
                icon="el-icon-delete"
                plain
                size="mini"
                type="danger"
                @click="deBounceWrap(handleDel, scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""
      ><em style="font-style: normal; color: red">删除后不可恢复</em
      >，你还要继续吗？</span
      >
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新增编辑弹窗 -->
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :title="titleOptions[title]"
      :visible.sync="dialogVisible"
      width="40%"
      @close="handleClear"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="签到天数" label-width="120px" prop="days">
          <el-input
            v-model="form.days"
            onkeypress="javascript:if(event.keyCode === 32)event.returnValue = false;"
            placeholder="请输入签到天数"
            style="width:220px"
          ></el-input>
        </el-form-item>
        <el-form-item label="礼包类型" prop="type">
          <el-radio-group v-model="form.type" @change="handleChange">
            <el-radio :label="1">道具包</el-radio>
            <el-radio :label="2">道具</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="道具Id" label-width="120px" prop="prop_id">
          <el-select v-model="form.prop_id" clearable placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="道具数量" label-width="120px" prop="number">
          <el-input
            v-model="form.number"
            onkeypress="javascript:if(event.keyCode === 32)event.returnValue = false;"
            placeholder="请输入道具数量"
            style="width:220px"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button plain type="primary" @click="handleClose">返回</el-button>
        <el-button type="primary" @click="deBounceWrap(handleSumbit, 'form')"
        >提交
        </el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  toGetList,
  delSign,
  toPostSign,
  toSignGift
} from '@/api/dailyCheck/dailyCheck'

export default {
  data() {
    return {

      data: [],
      titleOptions: {
        add: '新增',
        edit: '编辑'
      },
      title: 'add',
      rowData: {},
      options: [],
      form: {
        days: undefined,
        type: undefined,
        prop_id: undefined,
        number: undefined
      },
      rules: {
        days: [{ required: true, message: '请输入天数', trigger: 'blur' }],
        prop_id: [{ required: true, message: '请输入道具id', trigger: 'blur' }],
        number: [{ required: true, message: '请输入数量', trigger: 'blur' }],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }]
      },
      dialogVisible: false,
      //删除
      deleteDialogVisible: false,
      dataIds: undefined
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleChange(value) {
      if (value === 1) {
        this.form.number = 1
      } else {
        this.form.number = undefined
      }
      toSignGift({ type: value }).then(res => {
        this.options = res.data
      })
      this.options = []
      this.form.prop_id = undefined
    },

    //列表
    getList() {
      toGetList().then(res => {
        this.data = res.data.list
      })
    },
    handleAdd() {
      this.title = 'add'
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.title = 'edit'
      this.rowData = row
      this.form.number = this.rowData.number
      this.form.type = this.rowData.type
      this.form.days = this.rowData.days
      this.form.prop_id = this.rowData.prop_id
      toSignGift({ type: this.form.type }).then(res => {
        this.options = res.data
      })

      this.dialogVisible = true
    },
    handleSumbit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title === 'add') {
            let data = {
              prop_id: this.form.prop_id,
              type: this.form.type,
              number: this.form.number,
              days: this.form.days
            }
            toPostSign(data).then(res => {
              if (res.code === 200) {
                this.$message.success('提交成功')
                this.getList()
                this.dialogVisible = false
              } else {
              }
            })
          }
          if (this.title === 'edit') {
            let data1 = {
              id: this.rowData.id,
              prop_id: this.form.prop_id,
              type: this.form.type,
              number: this.form.number,
              days: this.form.days
            }

            toPostSign(data1).then(res => {
              if (res.code === 200) {
                this.$message.success('编辑成功')
                this.getList()
                this.dialogVisible = false
              }
            })
          }
        } else {
          return false
        }
      })
    },
    //清空表单
    handleClose() {
      this.dialogVisible = false
      this.$refs['form'].resetFields()
      this.$refs['form'].clearValidate()
    },
    handleClear() {
      this.$refs['form'].resetFields()
      this.$refs['form'].clearValidate()
      this.form = {
        days: undefined,
        type: undefined,
        prop_id: undefined,
        number: undefined
      }
      this.options = []
    },

    //删除
    handleDel(row) {
      this.dataIds = { id: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      delSign(this.dataIds).then(response => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false
          this.$notify({
            type: 'success',
            message: '操作成功'
          })
        }
      })
    }
  }
}
</script>
